const app = getApp()
Page({
  data: {
    spinShow: false,
    info: {}
  },

  onLoad: function(options) {
    this.loadUserInfo()
  },
  loadUserInfo() {
    let _this = this
    _this.setData({
      spinShow: true
    });
    app.formPost('/api/wx/student/user/current', null).then(res => {
      if (res.code == 1) {
        _this.setData({
          info: res.response
        });
      }else if(res.code == 401){

        wx.qy.login({
            success(wxres) {
                console.log("code是"+wxres.code)

              if (wxres.code) {
                app.formGet('/inApi/qywx/student/auth/appletLogin', {
                  "code": wxres.code
                }).then(res => {
                  if (res.code == 1) {
                    wx.setStorageSync('token', res.response.token)
                    wx.reLaunch({
                      url: '/pages/index/index',
                    });
                  }
                  else {
                    _this.message(res.message, '您无该系统的权限，请联系系统管理员')
    
                        wx.reLaunch({
                      url: '/pages/403/index',
                    });
                  }
                }).catch(e => {
                  _this.message(e, 'error')
                })  
              } else {
                _this.message(res.errMsg, 'error')
              }
            }
          })

      }else{
          
      }
      _this.setData({
        spinShow: false
      });
    }).catch(e => {
      _this.setData({
        spinShow: false
      });
      app.message(e, 'error')
    })
  },
  logOut() {
    let _this = this
    _this.setData({
      spinShow: true
    });
    app.formPost('/api/wx/student/auth/unBind', null).then(res => {
      if (res.code == 1) {
        wx.setStorageSync('token', '')
        wx.reLaunch({
          url: '/pages/user/bind/index',
        });
      }else if(res.code == 401){

        wx.qy.login({
            success(wxres) {
                console.log("code是"+wxres.code)

              if (wxres.code) {
                app.formGet('/inApi/qywx/student/auth/appletLogin', {
                  "code": wxres.code
                }).then(res => {
                  if (res.code == 1) {
                    wx.setStorageSync('token', res.response.token)
                    wx.reLaunch({
                      url: '/pages/index/index',
                    });
                  }
                  else {
                    _this.message(res.message, '您无该系统的权限，请联系系统管理员')
    
                        wx.reLaunch({
                      url: '/pages/403/index',
                    });
                  }
                }).catch(e => {
                  _this.message(e, 'error')
                })  
              } else {
                _this.message(res.errMsg, 'error')
              }
            }
          })

      }else{
          
      }
      _this.setData({
        spinShow: false
      });
    }).catch(e => {
      _this.setData({
        spinShow: false
      });
      app.message(e, 'error')
    })
  }
})